package star.包含min函数的栈;

import java.util.LinkedList;

public class MinStack {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(0);
        minStack.push(1);
        minStack.push(0);
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.min());
    }
    LinkedList<Integer> A;
    LinkedList<Integer> B;
    public MinStack() {
        A = new LinkedList<>();
        B = new LinkedList<>();
    }

    public void push(int x) {
        A.add(x);
        if (!B.isEmpty()){
            if (B.getLast()>=x){
                B.add(x);
            }
        }else {
            B.add(x);
        }
    }

    public void pop() {
        A.removeLast();
        if (A.getLast().equals(B.getLast())){
            B.removeLast();
        }
    }

    public int top() {
        return A.getLast();
    }

    public int min() {
        return B.getLast();
    }
}
